package me.zhengjie.modules.sync.processing;

import com.alibaba.fastjson.JSON;
import com.qiaofang.applet.common.constant.SyncTypeEnum;
import com.qiaofang.applet.service.property.domain.PropertyContact;
import com.qiaofang.applet.service.property.domain.PropertyVideo;
import com.qiaofang.applet.service.property.domain.SyncDataQueue;
import com.qiaofang.applet.service.property.service.PropertyContactService;
import com.qiaofang.applet.service.property.service.PropertyVideoService;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.sync.ProcessingBase;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/*
* 视频数据处理
* */
@Slf4j
@Service
public class PropertyContactSyncProcessing extends ProcessingBase {

    public PropertyContactSyncProcessing(){
        setDataType(SyncTypeEnum.PRO_CONTACT.getSyncType());
    }

    @Autowired
    PropertyContactService propertyContactService;

    @Override
    protected void processing(SyncDataQueue syncDataQueue) {
        String batchContent=syncDataQueue.getBatchContent();
        List<PropertyContact> list= JSON.parseArray(batchContent, PropertyContact.class);
        if(null==list){
            log.info("processing PropertyContact is null");
            return;
        }
        list.stream().forEach(item->{
            savePropertyContact(item,syncDataQueue);
        });
    }

    private void savePropertyContact(PropertyContact item,SyncDataQueue syncDataQueue){
        PropertyContact propertyContact= propertyContactService.findByContactIdAndPropertyUuidAndCompanyUuid(item.getContactId(),item.getPropertyUuid(),syncDataQueue.getCompanyUuid());
        if(!Objects.isNull(item.getDeleted()) && item.getDeleted()){
            if (!Objects.isNull(propertyContact)) {
                propertyContactService.deleteById(propertyContact.getId());
            }
            return;
        }
        if(Objects.isNull(propertyContact)){
            propertyContact=new PropertyContact();
        }
        //复制属性
        String[] ignoreProperties=new String[] {"id","createBy","updateBy","createTime","updateTime"};
        BeanUtils.copyProperties(item,propertyContact,ignoreProperties);
        propertyContact.setCompanyUuid(syncDataQueue.getCompanyUuid());
        propertyContact.setCityCode(syncDataQueue.getCityCode());
        propertyContact.setCorporationCode(syncDataQueue.getCorporationCode());
        propertyContact.setUpdateTime(new Date());
        propertyContact.setRelationUpdate(true);
        propertyContact.setLastSyncDate(new Date(System.currentTimeMillis() - DateUtil.DAY_MILLISECONDS));
        try {
            propertyContactService.save(propertyContact);
        } catch (Exception e) {
            log.error("savePropertyContact error",e);
            //记录错误日志
            this.saveErrorLog(JSON.toJSONString(propertyContact),e.getMessage(),syncDataQueue);
        }
    }
}
